<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>侦听器</title>
</head>
<body>
  <div id="app">
    用户名:<input type="text" v-model.lazy="user.username">
    电话号码: <input type="text" v-model="user.phone">
    <h2>{{msg_user}}</h2>
    <h2>{{msg_phone}}</h2>
  </div>
  <script src="vue.js"></script>
  <script>
    const vm = new Vue({
      el:"#app",
      data:{
        user:{
          phone:'',
          username:''
        },
        users:[
          {"username":"admin","phone":"114"},
          {"username":"kh","phone":"119"}
        ],
        msg_user:'',
        msg_phone:''
      },
      watch:{
        'user.username'(newVal,oldVal) {
          let index = this.users.findIndex(e=>e.username==newVal)
          if(index>=0) this.msg_user='用户名已经存在不能添加!' 
          else this.msg_user = "该用户可以注册"
        },
        'user.phone'(newVal,oldVal) {
          let index = this.users.findIndex(e=>e.phone==newVal)
          if(index>=0) {
            this.msg_phone = '电话已经存在'
          } else {
            this.msg_phone = '电话可以注册'
          }
        }
      }
    })
  </script>
</body>
</html>